package com.echo.register.service;

import com.echo.register.commons.proto.ShowInfoReq;
import com.echo.register.commons.proto.ShowInfoRespInfoGroup;
import com.echo.register.commons.redis.CadidateKey;
import com.echo.register.commons.redis.RedisClient;
import com.echo.register.domain.VoteCadidate;
import com.echo.register.mapper.CadidateMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: com.echo.voter
 * @description:候选人service
 * @author: Xiaoti
 * @create: 2018-11-05 14:58
 **/
@Service
public class CadidateService {

    private static Logger logger = LoggerFactory.getLogger(CadidateService.class);

    @Resource
    CadidateMapper cadidateMapper;
    @Resource
    RedisClient redisClient;

    public List<ShowInfoRespInfoGroup> showDetail(ShowInfoReq showInfoReq){
        List<ShowInfoRespInfoGroup> infoGroups = new ArrayList<>();
        List<VoteCadidate> cadidateList = new ArrayList<>();
        if (showInfoReq.getCadidateGroup().equals("") || showInfoReq.getCadidateGroup() == null) {
            return null;
        }

        List<String> groupKeys = (List<String>)redisClient.getList(showInfoReq.getCadidateGroup());
        if(groupKeys != null && !groupKeys.isEmpty()) {
            logger.info("groupKeys are {}", groupKeys.toString());
            for (String key : groupKeys) {
                VoteCadidate cadidate = redisClient.getObject(VoteCadidate.class, key);
                ShowInfoRespInfoGroup showInfoRespInfoGroup = new ShowInfoRespInfoGroup();
                showInfoRespInfoGroup.setCadidateName(cadidate.getCadidateName());
                showInfoRespInfoGroup.setCadidateDetail(cadidate.getCadidateDetail());
                showInfoRespInfoGroup.setCadidateId(cadidate.getCadidateId());
                showInfoRespInfoGroup.setCadidateTitle(cadidate.getCadidateTitle());
                showInfoRespInfoGroup.setCadidateUrl(cadidate.getCadidateUrl());
                showInfoRespInfoGroup.setCadidateValid(cadidate.getCadidateFlag());
                showInfoRespInfoGroup.setCadidateVotes(cadidate.getCadidateTotal());
                showInfoRespInfoGroup.setCadidateGroup(cadidate.getCadidateGroup());
                showInfoRespInfoGroup.setCadidatePdf(cadidate.getCadidatePdf());
                infoGroups.add(showInfoRespInfoGroup);
            }
        }else{
            cadidateList = cadidateMapper.getCadidateList(showInfoReq.getCadidateGroup());
            if(cadidateList.isEmpty()){
                return null;
            }
            logger.info("cadidates in redis is null, get from Database are {}", cadidateList.toArray().toString());
            for (VoteCadidate cadidate : cadidateList) {
                ShowInfoRespInfoGroup showInfoRespInfoGroup = new ShowInfoRespInfoGroup();
                showInfoRespInfoGroup.setCadidateName(cadidate.getCadidateName());
                showInfoRespInfoGroup.setCadidateDetail(cadidate.getCadidateDetail());
                showInfoRespInfoGroup.setCadidateId(cadidate.getCadidateId());
                showInfoRespInfoGroup.setCadidateTitle(cadidate.getCadidateTitle());
                showInfoRespInfoGroup.setCadidateUrl(cadidate.getCadidateUrl());
                showInfoRespInfoGroup.setCadidateValid(cadidate.getCadidateFlag());
                showInfoRespInfoGroup.setCadidateVotes(cadidate.getCadidateTotal());
                showInfoRespInfoGroup.setCadidateGroup(cadidate.getCadidateGroup());
                showInfoRespInfoGroup.setCadidatePdf(cadidate.getCadidatePdf());
                infoGroups.add(showInfoRespInfoGroup);
            }
            List<String> keys = new ArrayList<>();
            for (VoteCadidate cadidate : cadidateList) {
                redisClient.setexObjec(cadidate.getCadidateId(), cadidate, CadidateKey.TOKEN_EXPIRE);
                keys.add(cadidate.getCadidateId());
            }
            redisClient.setList(showInfoReq.getCadidateGroup(), keys, CadidateKey.TOKEN_EXPIRE);
        }
        return infoGroups;
    }

}
